/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CA = ComponentArt.Web.UI;
using ResourceManager = FourRoads.Links.Controls.ResourceManager;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for LinkModeration.
	/// </summary>
	public class LinkModeration : ClientPopulatedPopupMenu, IPostBackEventHandler 
	{
		protected override void OnLoad(EventArgs e) 
		{
			base.OnLoad(e);
		}
	
		CSContext csContext;
		bool canModerate = false;
		bool canDelete = false;
		bool canEdit = false;

		protected override void OnInit(EventArgs e) 
		{				
			csContext = CSContext.Current;
			this.ResourceName = "ModerationPopupMenu_Text";

			base.OnInit(e);
		}

		protected override ArrayList ClientMenuItemFormats
		{
			get
			{
				ArrayList formats = new ArrayList();
				ClientMenuItemFormat format;

				// 1 - sectionID, 2 - IsApproved, 3 - Comment User ID, 4 - Comment User ModerationLevel 
				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return (parseInt(parms[2]) != 1 && parseInt(parms[4]) == 1);')";
				format.Text = ResourceManager.GetString("ResLinkModerationMenu_Approve");
				format.ClientSideCommand = Page.GetPostBackClientEvent(this, "Approve:{0}:{1}");
				formats.Add(format);

				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return (parseInt(parms[6]) == 1);')";
				format.Text = ResourceManager.GetString("ResLinkModerationMenu_Move");
				format.NavigateUrl = ResLinkUrls.Instance().MoveLink(int.MaxValue  , int.MaxValue-1 , CSContext.Current.RawUrl ).Replace(int.MaxValue.ToString() , "{0}" ).Replace((int.MaxValue-1).ToString(), "{1}"); ///move link redirect to moderation move link page coppy from thread move page
				formats.Add(format);

				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return (parseInt(parms[6]) == 1);')";
				format.Text = ResourceManager.GetString("ResLinkModerationMenu_Edit");
				format.ClientSideCommand = "javascript:EditLink({0} , {1});";
				formats.Add(format);
		
				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return parseInt(parms[5]) == 1;')";
				format.Text = ResourceManager.GetString("ResLinkModerationMenu_Delete");
				format.ClientSideCommand = Page.GetPostBackClientEvent(this, "Delete:{0}:{1}");
				formats.Add(format); 

				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return parseInt(parms[3]) == 1;')";
				format.Text = ResourceManager.GetString("ResLinkModerationMenu_Enable");
				format.ClientSideCommand = Page.GetPostBackClientEvent(this, "Enable:{0}:{1}");
				formats.Add(format); 

				return formats;
			}
		}
	
		protected override void OnPreRender(EventArgs e)
		{
			Visible = (UserHasAccess);

			if (Visible)
			{
				if (Parameters.Length != 7)
				{
					this.Parameters = new string[7];
					this.Parameters[0] = this.Post.SectionID.ToString();
					this.Parameters[1] = this.Post.PostID.ToString();
					this.Parameters[2] = this.Post.IsApproved ? "1" : "0";
					this.Parameters[3] = this.Post.ReportedDead ? "1" : "0";
					this.Parameters[4] = canModerate ? "1" : "0";
					this.Parameters[5] = canDelete ? "1" : "0";
					this.Parameters[6] = canEdit ? "1" : "0";
				}

				base.OnPreRender(e);
			}
		}


		private ResLinkLink post = null;
		public ResLinkLink Post 
		{
			get 
			{
				return post;
			}
			set 
			{
				post = value;
			}
		}

		private ResLinkSection section = null;
		public ResLinkSection Section 
		{
			get 
			{
				return section;
			}
			set 
			{
				section = value;
			}
		}

		public bool UserHasAccess
		{
			get
			{
				if (ViewState["UserHasAccess"] == null)
				{
					if (Section != null)
					{
						canModerate = Permissions.ValidatePermissions(Section, (Permission)ResLinkPermissionEnum.Moderate , csContext.User)
							|| ExtendedUserRoles.IsLinkAdministrator || CSContext.Current.User.IsModerator;

						canDelete = Permissions.ValidatePermissions(Section, (Permission)ResLinkPermissionEnum.RemoveLinks , csContext.User);
						canEdit = Permissions.ValidatePermissions(Section, (Permission)ResLinkPermissionEnum.EditLinks , csContext.User);

						ViewState["UserHasAccess"] = (canModerate || canDelete || canEdit);
					}
					else
					{
						ViewState["UserHasAccess"] = false;
					}
				}
				return (bool)ViewState["UserHasAccess"];
			}
		}


		#region Event Handlers

		// *********************************************************************
		//  Approve_Click
		//
		/// <summary>
		/// Event handler for approving a post
		/// </summary>
		// ***********************************************************************/
		private void Approve_Click(string section , string postID ) 
		{   
			ResLinkLink lnk = ResLinkLink.GetSingleLink(System.Convert.ToInt32(postID));

			ResLinkSection sect = ResLinkSection.GetSection(CSContext.Current.GetIntFromQueryString("SectionID" , -1));
			sect.ApproveLink(lnk);
		
			this.Post = lnk;
			//Now we need to get the parent repeater, this is a tempoary version until
			//the ajax version comes along
			Repeater rpt = Parent.Parent.Parent.Parent.Parent as Repeater;
			if (rpt != null)
			{
				rpt.DataSource = sect.Links;
				rpt.DataBind();
			}
		}

		// *********************************************************************
		//  Enable_Click
		//
		/// <summary>
		/// Event handler for re-enabling a dead link
		/// </summary>
		// ***********************************************************************/
		private void Enable_Click(string section , string postID ) 
		{   
			ResLinkLink lnk = ResLinkLink.GetSingleLink(System.Convert.ToInt32(postID));

			ResLinkSection sect = ResLinkSection.GetSection(CSContext.Current.GetIntFromQueryString("SectionID" , -1));
		
			lnk.ReportedDead = false;

			sect.UpdateLink(lnk);
		
			this.Post = lnk;
			//Now we need to get the parent repeater, this is a tempoary version until
			//the ajax version comes along
			Repeater rpt = Parent.Parent.Parent.Parent.Parent as Repeater;
			if (rpt != null)
			{
				rpt.DataSource = sect.Links;
				rpt.DataBind();
			}
		}


		private void Delete_Click(string section , string postID ) 
		{   
			ResLinkSection sect = ResLinkSection.GetSection(CSContext.Current.GetIntFromQueryString("SectionID" , -1));

			sect.DeleteLink(System.Convert.ToInt32( postID ));

			Repeater rpt = Parent.Parent.Parent.Parent.Parent as Repeater;
			if (rpt != null)
			{
				rpt.DataSource = sect.Links;
				rpt.DataBind();
			}
		}
		#endregion

		#region IPostBackEventHandler Members

		public void RaisePostBackEvent(string eventArgument)
		{
			string[] e = eventArgument.Split(':');
			switch (e[0])
			{
				case "Approve":
					this.Approve_Click(e[1] , e[2]);
					break;

				case "Delete":
					this.Delete_Click(e[1] , e[2]);
					break;

				case "Enable":
					this.Enable_Click(e[1] , e[2]);
					break;
			}	
		}

		#endregion
	}
}
